#include <iostream>
#include <stdio.h>
#include <thread>
#include <vector>
#include <string>
#include <list>
#include <mutex>
#include <future>
#include <map>
#include <atomic>
using namespace std;

//一般atomic原子操作，针对++，–，+=，&=，|=，^=是支持的

/*
不能写成std::atomic<int> g_mycount = 0；，否则
error: use of deleted function ‘std::atomic<int>::atomic(const std::atomic<int>&)’
std::atomic<int> g_mycount = 0;
*/

atomic<int> g_count;//每次都是2000符合逻辑
//int  g_count=0;//会出现好几种结果不一定是2000
//th2 end:1999
//all over1999
//th2 end:2000
//all over:2000


void fun()
{
    for(int i=0;i<1000;i++)
    {
        cout<<this_thread::get_id()<<"   fun is  working"<<endl;
        //this_thread::sleep_for(chrono::seconds(1));
        g_count++;
        cout<<this_thread::get_id()<<"   i:"<<i <<endl;
    }
    return;
}

int main()
{
    //cout << "Hello World!" << endl;
    thread th1(fun);
    thread th2(fun);
    th1.join();
    cout<<"th1 end:"<<g_count<<endl;
    th2.join();
    cout<<"th2 end:"<<g_count<<endl;
    cout<<"all over"<<g_count<<endl;
    return 0;
}
